/** @file GroupSwitch.qml
@brief Grouped set of switches. It supports up to 10 rows of different switches.

This object uses recursively the GroupLoader. It is sufficient to define one of the objects
to an empty string to ingore the element.
For the same reason to destroy all the elements the calling program should reset to "" (empty
string) \e all the GroupLoader.obj properties. In the same way is possible a selective
loading/unloading of some of the objects in the group.

@authorname \n
@licenseversion \n
@actualversion - @dateupdate

@licensenote
*/

import QtQuick 1.0
import "Theme.js" as JObj
import "Math.js"as JMath
import "./" as Framework

Item {
    id: gSwitch

    width: parent.width
    height: parent.height

    // Field names
    property string sw1_field: ""; property string sw2_field: ""; property string sw3_field: "";
    property string sw4_field: ""; property string sw5_field: ""; property string sw6_field: "";
    property string sw7_field: ""; property string sw8_field: ""; property string sw9_field: "";
    property string sw10_field: "";
    // Info names
    property string sw1_info: ""; property string sw2_info: ""; property string sw3_info: "";
    property string sw4_info: ""; property string sw5_info: ""; property string sw6_info: "";
    property string sw7_info: ""; property string sw8_info: ""; property string sw9_info: "";
    property string sw10_info: "";
    // Object names
    property string sw1_obj: ""; property string sw2_obj: ""; property string sw3_obj: "";
    property string sw4_obj: ""; property string sw5_obj: ""; property string sw6_obj: "";
    property string sw7_obj: ""; property string sw8_obj: ""; property string sw9_obj: "";
    property string sw10_obj: "";
    // Initial switch states
    property bool sw1_state: false; property bool sw2_state: false; property bool sw3_state: false;
    property bool sw4_state: false; property bool sw5_state: false; property bool sw6_state: false;
    property bool sw7_state: false; property bool sw8_state: false; property bool sw9_state: false;
    property bool sw10_state: false;
    // Info button status
    property bool sw1_infoshow: false; property bool sw2_infoshow: false; property bool sw3_infoshow: false;
    property bool sw4_infoshow: false; property bool sw5_infoshow: false; property bool sw6_infoshow: false;
    property bool sw7_infoshow: false; property bool sw8_infoshow: false; property bool sw9_infoshow: false;
    property bool sw10_infoshow: false;

    property string grouptitle:  ""             // Text of the title
    property string themeString: ""             // Theme name

    // Geometry dependent properties (default values are overriddend by the applicaiton)
    property int switchHeigth: 45               // Height of every object
    property int fontPoints: 10                 // Base text font size in points
    property int fontSwitches: 8                // Switch text description font size in points
    property int fieldSpacing: 5                // Distance between fields
    property int fieldSeparator: 20             // Distance between title and fields
    // The current option contains the option value selected by the user
    property int currentOption: 1

    // ------------------------------------------------------------ FUNCTIONS
    // The function to dinamically update the state options of the group.
    function updateOptions(optionNumber) {
        if (currentOption != optionNumber) {
            switchStateOption(currentOption, false);
            currentOption = optionNumber;
        }
        else {
            switchStateOption(optionNumber, true);
        }
    }

    function switchStateOption(flagOption, flagSet) {
        switch(flagOption) {
        case 1: option1.obj_flag = flagSet; break;
        case 2: option2.obj_flag = flagSet; break;
        case 3: option3.obj_flag = flagSet; break;
        case 4: option4.obj_flag = flagSet; break;
        case 5: option5.obj_flag = flagSet; break;
        case 6: option6.obj_flag = flagSet; break;
        case 7: option7.obj_flag = flagSet; break;
        case 8: option8.obj_flag = flagSet; break;
        case 9: option9.obj_flag = flagSet; break;
        case 10: option10.obj_flag = flagSet; break;
        }
    }

    function getObjFullName(objNam) {
        if (objNam == "") {
            return "";
        }
        else {
            return objNam + ".qml";
        }
    }
    // ------------------------------------------------------------ FUNCTIONS END

    // Title of the group
    Text {
        id: mainTitle
        text: grouptitle; anchors.top: parent.top; anchors.horizontalCenter: parent.horizontalCenter
        color: JMath.sw_TitleColor; font.pointSize: fontPoints; font.bold: true
    }

    // Switch elements
    Column {
        id: columnFlags
        width: parent.width; spacing: fieldSpacing
        anchors.top: mainTitle.bottom; anchors.topMargin: fieldSeparator

        Framework.GroupLoader {
            id: option1
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw1_obj); obj_string: sw1_field
            obj_message: sw1_info; obj_flag: true; obj_info: sw1_infoshow
            onSwitched: { updateOptions(1); }
        }
        Framework.GroupLoader {
            id: option2
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw2_obj); obj_string: sw2_field
            obj_message: sw2_info; obj_flag: false; obj_info: sw2_infoshow
            onSwitched: { updateOptions(2); }
        }
        Framework.GroupLoader {
            id: option3
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw3_obj); obj_string: sw3_field
            obj_message: sw3_info; obj_flag: false; obj_info: sw3_infoshow
            onSwitched: { updateOptions(3); }
        }
        Framework.GroupLoader {
            id: option4
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw4_obj); obj_string: sw4_field
            obj_message: sw4_info; obj_flag: false; obj_info: sw4_infoshow
            onSwitched: { updateOptions(4); }
        }
        Framework.GroupLoader {
            id: option5
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw5_obj); obj_string: sw5_field
            obj_message: sw5_info; obj_flag: false; obj_info: sw5_infoshow
            onSwitched: { updateOptions(5); }
        }
        Framework.GroupLoader {
            id: option6
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw6_obj); obj_string: sw6_field
            obj_message: sw6_info; obj_flag: false; obj_info: sw6_infoshow
            onSwitched: { updateOptions(6); }
        }
        Framework.GroupLoader {
            id: option7
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw7_obj); obj_string: sw7_field
            obj_message: sw7_info; obj_flag: false; obj_info: sw7_infoshow
            onSwitched: { updateOptions(7); }
        }
        Framework.GroupLoader {
            id: option8
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw8_obj); obj_string: sw8_field
            obj_message: sw8_info; obj_flag: false; obj_info: sw8_infoshow
            onSwitched: { updateOptions(8); }
        }
        Framework.GroupLoader {
            id: option9
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw9_obj); obj_string: sw9_field
            obj_message: sw9_info; obj_flag: false; obj_info: sw9_infoshow
            onSwitched: { updateOptions(9); }
        }
        Framework.GroupLoader {
            id: option10
            height: switchHeigth; width: parent.width
            theme: "default"; obj: getObjFullName(sw10_obj); obj_string: sw10_field
            obj_message: sw10_info; obj_flag: false; obj_info: sw10_infoshow
            onSwitched: { updateOptions(10); }
        }
    }
}
